<?php
/* Description: This file is used to add sub sub category. */
#########IncludeFiles####################################################################################
include_once '../inc/config.php';
include_once("../inc/bootstrape.php");
require_once _SITE_PATH_.'classes/Vendor/Vendor.php';
require_once _SITE_PATH_.'classes/Category/Category.php';
require_once _SITE_PATH_.'classes/CommonUtility/CommonUtility.php';

$category_id = isset($_REQUEST['category_id'])?$_REQUEST['category_id']:'';
$id = trim($_REQUEST['id']); 
$cancleURL = _SITE_ADMIN_URL_.'category/subsubcategory_mng.php?category_id='.$category_id ;

if( $_POST['submit']=='Submit'){    
    $parms['name'] = $_POST['name'];
	//$parms['id'] = $_POST['id'];
	$category_id = $_POST['category_id'];
	//$parms['parent_id'] = $_POST['parent_id'];
	$parms['depth'] = 2;
	$parms['added'] = date("Y-m-d h:i:s");
	$parms['modified'] = date("Y-m-d h:i:s");;
        
    $condition = array(); 
	$condition['id'] = $_POST['id'];
	if( CommonUtility::updateData($parms, 'category', $condition)) {        
        //  delete existing attributes
        $parms = '';
        $parms['category_id'] = $id;
        CommonUtility::deleteData('category_attributes', $parms);
        
        // add new attributes to the table
        $parms['added'] = date("Y-m-d h:i:s");
        foreach( $_POST['attributes'] as $i => $val ){
            $parms['attribute_id'] = $val;            
            $parms['unit_id'] = $_POST['unit'][$i];
            $parms['required'] = '0';
            if( $_POST['comp_'.$i] == 1){
                $parms['required'] = '1';
            }
            CommonUtility::insertData($parms, 'category_attributes');
        }
        
        header("Location: subsubcategory_mng.php?category_id=".$category_id."&msg=1100");            
        exit;
    }
    $err = 1101;    
}

if(!empty($id) || !empty($category_id)){
	$parent_id = Category::getParentId($category_id);
	$categoryRow = CommonUtility::getDataById($category_id, 'category');
	$parentRow = CommonUtility::getDataById($parent_id, 'category');

	$sub_sub_categories = array();
	$sub_sub_categories = Category::getSubSubCategories($id,'id');
	$result = $sub_sub_categories[0];

	$condition['status']='1';
	$attribute = CommonUtility::getData($parms=null, $table='attribute_master', $condition);
	foreach($attribute as $row){
		$attributeStr .= '<option value="'.$row['id'].'">'.$row['title'].'</option>';
	}

	$unitRec = CommonUtility::getData($parms=null, $table='unit_master', $condition);
	foreach($unitRec as $row){
		$unitStr .= '<option value="'.$row['id'].'">'.$row['title'].'</option>';
	}

	$condition = null;
	$condition['category_id'] = $id;
	$catAttributeRow = CommonUtility::getData($parms=null, $table='category_attributes', $condition);
	$errMsg = $err;
	$_tplName = 'category/subsubcategory_edit.tpl';
	$_title = 'Edit Sub Sub-Category';
}
require_once _ADMIN_TPL_MAIN_;
?>